package com.hardy.leetcode;

/**
 * Follow up for "Remove Duplicates":
 What if duplicates are allowed at most twice?
 For example,
 Given sorted array A =[1,1,1,2,2,3],
 Your function should return length =5, and A is now[1,1,2,2,3].
 * User: Ruijie Date: 2016/9/20
 */
public class RemoveDuplicatesFromSortedArrayLi {
    public static int removeDuplicates(int[] A) {
        if(A.length<1){
            return 0;
        }
        int slow=2,fast;
        for(fast=2;fast<A.length;fast++){
            if(A[fast]!=A[slow-2]){
                A[slow++]=A[fast];
            }
        }
        return slow;
    }

    public static void main(String[] args) {
        int[] arr={1,1,1,1,2,2,2,2,3,3,3,4,4,4,4,4,5,5,5,6,7,7,8,9};
        System.out.println(removeDuplicates(arr));
    }
}
